﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace MVCCricketWorldCupTrivia.Models
{
    public class Game
    {  
        #region Properties

        public int SriLankaScore { get; set; }   
 
        public int Wickets { get; set; }   

        public int Runs { get; set; }    

        public int Overs { get; set; }

        #endregion

        #region public methods

        public void CreateGame()
        {
            if (System.Web.HttpContext.Current.Session["Game"] == null)
            {
                Runs = 0;
                Wickets = 0;
                Overs = 0;
                SriLankaScore = GameSettings.SriLankaScore;
                System.Web.HttpContext.Current.Session["Game"] = this;
            }
        }

        public Question GetNextQuestion()
        {
            Random r = new Random();
            int questionID = r.Next(GameSettings.RandomQuestionsPerLevel);

            Question question = Question.Questions.SingleOrDefault(
                q => q.QuestionID == questionID);
            
            return question;
        }

        public ShotResult GetResult(Question questionAnswered)
        {
            Question question = Question.Questions.SingleOrDefault(
             q => q.QuestionID == questionAnswered.QuestionID &&
             String.Compare(questionAnswered.PlayersAnswer, q.Answer, true)!=-1);

            return (question != null) ? new ShotResult { 
                Out = false,
                Runs = (int)Math.Pow((GameSettings.BaseRunValue),question.Level) }
                                      : new ShotResult{Out=true,Runs=0};
        }

        public bool IsGameActive()
        {
            return System.Web.HttpContext.Current.Session["Game"] == null ? false : true;
        }

        #endregion
    }
}